package com.aplink.generic.pojo.controlspec.actionbar;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.menu.MenuItemSpec;
import com.aplink.generic.pojo.formdesign.ActionBarButton;
import com.aplink.generic.pojo.formdesign.OptionMenu;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.sg.R;

public class ActionBarSpec extends ControlSpec {
	private static final long serialVersionUID = -8085514510572970934L;
	private boolean isDisplayHomeAsUp;
	private boolean isDisplayShowHomeEnabled;
	private boolean isDisplayShowTitleEnabled;
	private ActionBarButton mActionBarButton;
	private String mIcon;
	private OptionMenu mOptionMenu;
	private SearchViewSpec mSearchViewSpec;
	private SelectionSpec mSelectionSpec;
	private String mSubTitle;
	private String mTitle;

	public ActionBarSpec() {
		mActionBarButton = new ActionBarButton();
		isDisplayShowTitleEnabled = true;
		setOptionMenu(new OptionMenu());
	}

	public LinearLayout getActionBarButton(final GenericFragment genericFragment) {
		final Activity activity = genericFragment.getActivity();
		final LinearLayout root = (LinearLayout) activity.getLayoutInflater()
		        .inflate(R.layout.aplink__custom_actionbar,
		                new LinearLayout(activity), false);
		for (int i = 0; i < this.mActionBarButton.size(); i++) {
			final LinearLayout linearLayout = (LinearLayout) activity
			        .getLayoutInflater().inflate(
			                R.layout.aplink__custom_actionbar_button,
			                new LinearLayout(activity), false);
			final MenuItemSpec menuItemSpec = this.mActionBarButton.valueAt(i);
			if (menuItemSpec.getIconID() != ResourcesHelper.RESOURCE_NOT_FOUND) {
				final ImageView imageView = (ImageView) activity
				        .getLayoutInflater().inflate(
				                R.layout.aplink__custom_actionbar_button_image,
				                new LinearLayout(activity), false);
				imageView.setImageResource(menuItemSpec.getIconID());
				linearLayout.addView(imageView);
			}
			if (!TextUtils.isEmpty(menuItemSpec.getTitle())) {
				final TextView textView = (TextView) activity
				        .getLayoutInflater().inflate(
				                R.layout.aplink__custom_actionbar_button_text,
				                new LinearLayout(activity), false);
				textView.setText(menuItemSpec.getTitle());
				linearLayout.addView(textView);
			}
			linearLayout.setTag(menuItemSpec);
			linearLayout.setOnClickListener(genericFragment
			        .getOnActionbarButtonClickListener());
			root.addView(linearLayout);
		}
		return root;
	}

	public ActionBarButton getActionBarButtons() {
		return mActionBarButton;
	}

	public String getIcon() {
		return this.mIcon;
	}

	public int getIconID() {
		if (TextUtils.isEmpty(this.mTitle)) {
			return ResourcesHelper.RESOURCE_NOT_FOUND;
		}
		return ResourcesHelper.getDrawableIdByName(this.mIcon);
	}

	public OptionMenu getOptionMenus() {
		return mOptionMenu;
	}

	public SearchViewSpec getSearchViewSpec() {
		return this.mSearchViewSpec;
	}

	public SelectionSpec getSelectionSpec() {
		return this.mSelectionSpec;
	}

	public String getSubTitle() {
		return this.mSubTitle;
	}

	public String getTitle() {
		return this.mTitle;
	}

	public boolean isDisplayHomeAsUp() {
		return this.isDisplayHomeAsUp;
	}

	public boolean isDisplayShowHomeEnabled() {
		return isDisplayShowHomeEnabled;
	}

	public boolean isDisplayShowTitleEnabled() {
		return isDisplayShowTitleEnabled;
	}

	public void setActionBarButtons(ActionBarButton actionBarButton) {
		mActionBarButton = actionBarButton;
	}

	public void setDisplayHomeAsUp(final boolean isDisplayHomeAsUp) {
		this.isDisplayHomeAsUp = isDisplayHomeAsUp;
	}

	public void setDisplayShowHomeEnabled(final boolean isDisplayShowHomeEnabled) {
		this.isDisplayShowHomeEnabled = isDisplayShowHomeEnabled;
	}

	public void setDisplayShowTitleEnabled(
	        final boolean isDisplayShowTitleEnabled) {
		this.isDisplayShowTitleEnabled = isDisplayShowTitleEnabled;
	}

	public void setIcon(final String icon) {
		this.mIcon = icon;
	}

	public void setOptionMenu(OptionMenu optionMenu) {
		mOptionMenu = optionMenu;
	}

	public void setSearchViewSpec(final SearchViewSpec searchViewSpec) {
		this.mSearchViewSpec = searchViewSpec;
	}

	public void setSelectionSpec(final SelectionSpec selectionSpec) {
		this.mSelectionSpec = selectionSpec;
	}

	public void setSubTitle(final String subTitle) {
		this.mSubTitle = subTitle;
	}

	public void setTitle(final String title) {
		this.mTitle = title;
	}
}
